//
//  XFCustomSlider.m
//  FreeTime
//
//  Created by 王江华 on 16/9/19.
//  Copyright © 2016年 SRDZ.com. All rights reserved.
//

#import "XFCustomSlider.h"

@implementation XFCustomSlider



- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:CGRectMake(frame.origin.x, frame.origin.y, (frame.size.width < 48?48:frame.size.width), 24)];
    if (self) {
        // Initialization code
        self.backgroundColor = [UIColor clearColor];
        
        _valueMin = 0;
        _valueMinCurrent = 0;
        _valueMax = 100;
        _valueMaxCurrent = 100;
        
        CGFloat barHeight = 2;
        CGFloat barLeft = 0;
        CGFloat barTop = (self.frame.size.height - barHeight)/2;
        CGFloat barWidth = self.frame.size.width;
        
        //设置背景bar的图片
        _imageSliderBGGray = [[UIImageView alloc] initWithFrame:CGRectMake( barLeft, barTop, barWidth, barHeight)];
        UIImage *imageBG = [[UIImage imageNamed:@"Ovalgray"] stretchableImageWithLeftCapWidth:5 topCapHeight:5];
        [_imageSliderBGGray setImage:imageBG];
        [self addSubview:_imageSliderBGGray];
        
        //设置上面蓝色bar的图片
        _imageSliderBGBlue = [[UIImageView alloc] initWithFrame:_imageSliderBGGray.frame];
        UIImage *imageLeftBG = [[UIImage imageNamed:@"Oval"] stretchableImageWithLeftCapWidth:5 topCapHeight:5];
        [_imageSliderBGBlue setImage:imageLeftBG];
        [self addSubview:_imageSliderBGBlue];
        
        
        //设置左侧按钮
        _buttonLeft = [[XFDraggableButton alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.height, self.frame.size.height)];
        [_buttonLeft setBackgroundImage:[UIImage imageNamed:@"btn_sliden_bar"] forState:UIControlStateNormal];
        _buttonLeft.limitLeft = 0;
        _buttonLeft.limitRight = self.frame.size.width - self.frame.size.height*2.0;
        [_buttonLeft setDelegate:self];
        [self addSubview:_buttonLeft];
        //设置右侧按钮
        _buttonRight = [[XFDraggableButton alloc] initWithFrame:CGRectMake(barWidth - self.frame.size.height, 0, self.frame.size.height, self.frame.size.height)];
        [_buttonRight setBackgroundImage:[UIImage imageNamed:@"btn_sliden_bar"] forState:UIControlStateNormal];
        _buttonRight.limitLeft = self.frame.size.height;
        _buttonRight.limitRight = self.frame.size.width - self.frame.size.height;
        [_buttonRight setDelegate:self];
        [self addSubview:_buttonRight];
        
        
    }
    return self;
}

#pragma mark - Set View
//设置对应Button的Frame
-(void)setPositionForButton:(XFDraggableButton *)button{
    CGFloat buttonWidth = _buttonLeft.frame.size.width;
    CGFloat sliderWidth = self.frame.size.width - buttonWidth*2;
    CGFloat interval = _valueMax - _valueMin;
    
    CGRect frame = button.frame;
    if (button == _buttonLeft) {
        frame.origin.x = sliderWidth*(_valueMinCurrent/interval);
        
        NSLog(@"qq%f",frame.origin.x);
        
    }else{
        frame.origin.x = sliderWidth*(_valueMaxCurrent/interval) + buttonWidth;
        NSLog(@"qqq%f",frame.origin.x);
    }
    [button setFrame:frame];
    [self setFrameForSliderBlue];
}

//为背景，蓝色条设置Frame
-(void)setFrameForSliderBlue{
    CGRect frame = _imageSliderBGBlue.frame;
    frame.origin.x = _buttonLeft.frame.origin.x;
    frame.size.width = _buttonRight.frame.origin.x + _buttonRight.frame.size.width - _buttonLeft.frame.origin.x;
    
    NSLog(@"dddddssawdddddd%f",frame.size.width);
    
    NSLog(@"123456%f",_buttonLeft.frame.origin.x);
    
    NSLog(@"123aa%f",_buttonRight.frame.origin.x);
    [_imageSliderBGBlue setFrame:frame];
}

#pragma mark - Setter method

-(void)setValueMin:(NSInteger)valueMin{
    if (valueMin > _valueMax) {
        _valueMax = valueMin;
    }
    _valueMin = valueMin;
}

-(void)setValueMax:(NSInteger)valueMax{
    if (valueMax < _valueMin) {
        _valueMin = valueMax;
    }
    _valueMax = valueMax;
}

-(void)setValueMinCurrent:(NSInteger)valueMinCurrent{
    if (valueMinCurrent < _valueMin) {
        valueMinCurrent = _valueMin;
    }
    
    if (valueMinCurrent > _valueMax) {
        valueMinCurrent = _valueMax;
    }
    
    if (valueMinCurrent > _valueMaxCurrent) {
        _valueMaxCurrent = valueMinCurrent;
        [self setPositionForButton:_buttonRight];
    }
    
    _valueMinCurrent = valueMinCurrent;
    [self setPositionForButton:_buttonLeft];
}

-(void)setValueMaxCurrent:(NSInteger)valueMaxCurrent{
    if (valueMaxCurrent > _valueMax) {
        valueMaxCurrent = _valueMax;
    }
    
    if (valueMaxCurrent < _valueMin) {
        valueMaxCurrent = _valueMin;
    }
    
    if (valueMaxCurrent < _valueMinCurrent) {
        _valueMinCurrent = valueMaxCurrent;
        [self setPositionForButton:_buttonLeft];
    }
    
    _valueMaxCurrent = valueMaxCurrent;
    [self setPositionForButton:_buttonRight];
}

#pragma mark - Private
//设置当前两个按钮，分别对应的值
-(void)setCurrentValue{
    CGFloat buttonWidth = _buttonLeft.frame.size.width;
    CGFloat sliderWidth = self.frame.size.width - buttonWidth*2;
    CGFloat interval = _valueMax - _valueMin;
    
    CGFloat leftButtonValue = _buttonLeft.frame.origin.x;
    CGFloat rightButtonValue = _buttonRight.frame.origin.x - _buttonRight.frame.size.width;
    
    _valueMinCurrent = interval * (leftButtonValue/sliderWidth);
    _valueMaxCurrent = interval *(rightButtonValue/sliderWidth);
    
    NSLog(@"_valueMinCurrent is %d",_valueMinCurrent);
    NSLog(@"_valueMaxCurrent is %d",_valueMaxCurrent);
    
    //调用代理类的方法，修改对应Slider的 最小值 和 最大值
    if (_delegate && [_delegate respondsToSelector:@selector(customSliderValueChanged:)]) {
        [_delegate customSliderValueChanged:self];
    }
}

#pragma mark - HXLDraggableButtonDelegate
-(void)dragWithButton:(XFDraggableButton *)button{
    
    if (_buttonRight.frame.origin.x < _buttonLeft.frame.origin.x + _buttonRight.frame.size.width) {
        CGRect frame = button.frame;
        if (button == _buttonRight) {
            frame.origin.x -= _buttonRight.frame.size.width;
            [_buttonLeft setFrame:frame];
        }else{
            frame.origin.x += _buttonLeft.frame.size.width;
            [_buttonRight setFrame: frame];
        }
    }
    
    [self setFrameForSliderBlue];
    
    [self setCurrentValue];
}
@end
